package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TeacherPracticeRead;
import com.yg.entity.singleEntity.TeacherPractice;
import com.yg.service.TeacherPracticeService;
import com.yg.utils.CloseStream;
import com.yg.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 对应sheet04，此表是教师实践锻炼表
 注意！此表的id和教师表的id是一个id 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/teacherPractice")
@CrossOrigin
public class TeacherPracticeController {

    @Autowired
    TeacherPracticeService teacherPracticeService;

    /**
     * 查询所有
     * clr
     *
     * @return
     */
    @GetMapping("/selectAllTeacherPracticeInfo")
    public R selectAllTeacherPracticeInfo(HttpServletRequest httpServletRequest,@RequestParam("current") int current,@RequestParam("limit") int limit) {
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        String teacherIdByJwtToken = JwtUtils.getTeacherIdByJwtToken(httpServletRequest);
        R r = teacherPracticeService.selectAllTeacherPracticeInfo(current,limit,vipByJwtToken,teacherIdByJwtToken);
        return r;
    }


    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file, HttpServletRequest httpServletRequest) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        //判断权限
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        if ("v2".equals(vipByJwtToken)){
            return R.ERROR().message("警告：请以管理员身份登录");
        }
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = teacherPracticeService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭fileInputStream
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }


    /**
     * 导出excel
     * clr
     *
     * @return
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletRequest httpServletRequest,HttpServletResponse response ,@RequestParam(value = "name",required = false)String name,@RequestParam(value = "title",required = false)String title,@RequestParam(value = "education",required = false)String education) throws UnsupportedEncodingException {
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        TeacherPracticeRead teacherPracticeRead = new TeacherPracticeRead();
        teacherPracticeRead.setName(name);
        teacherPracticeRead.setTitle(title);
        teacherPracticeRead.setEducation(education);
        return "v1".equals(vipByJwtToken) ? teacherPracticeService.writeExcel(response,teacherPracticeRead) : R.ERROR().message("警告：请以管理员身份登录");
    }


    /**
     *  条件查询
     *  clr
     * @return
     */
    @PostMapping("/teacherPracticeQuery")
    public R teacherPracticeQuery(HttpServletRequest httpServletRequest,@RequestBody String teacherCondition,@RequestParam("current") int current,@RequestParam("limit") int limit){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherPracticeService.teacherConditionalQuery(vipByJwtToken,teacherCondition,current,limit) : R.ERROR().message("警告：请以管理员身份登录");
    }

    /**
     * 根据id更新教师信息
     * @return
     */
    @PutMapping("/updateTeacherInfo")
    public R updateTeacherInfo(HttpServletRequest httpServletRequest,@RequestBody String teacherJson){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherPracticeService.updateTeacherInfo(teacherJson) : R.ERROR().message("警告：请以管理员身份登录");
    }

    /**
     * 根据id删除教师信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTeacherById")
    public R deleteTeacherById(HttpServletRequest httpServletRequest,@RequestBody String id){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherPracticeService.deleteTeacherById(id) : R.ERROR().message("警告：请以管理员身份登录");
    }

    /**
     * 添加一个教研项目
     * @param teacherPractice
     * @return
     */
    @PostMapping("/insertTeacherPractice")
    public R insertTeacherPractice(HttpServletRequest httpServletRequest,@RequestBody TeacherPractice teacherPractice){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherPracticeService.insertTeacherPractice(teacherPractice) : R.ERROR().message("警告：请以管理员身份登录");
    }
}

